package com.lanxin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * Created by cyh on 2021/3/4.
 */
@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("listorder")
    public String listorder(){

        // 利用RestTemplate发送基于http的Rest请求
        ServiceInstance serviceInstance = loadBalancerClient.choose("empservice");
        String ip = serviceInstance.getHost();
        Integer port = serviceInstance.getPort();
        String url = "http://"+ip+":"+port+"/emp/listemp";
        String str = restTemplate.getForObject(url,String.class);
        return "listorder "+str;
    }
}
